//
// Created by yfux on 2022/4/18.
//

#ifndef ICANLIVE_VIDEOPLAYER_H
#define ICANLIVE_VIDEOPLAYER_H

#define AV_SYNC_THRESHOLD_MIN 0.04
#define AV_SYNC_THRESHOLD_MAX 0.1

#include "BasePlayer.h"
#include "AudioPlayer.h"
#include <pthread.h>
#include <android/native_window.h>

class VideoPlayer : public BasePlayer {
    friend void *vdecode_t(void *args);

    friend void *vplay_t(void *args);

public:
    VideoPlayer();

    VideoPlayer(int channelId, JavaCallHelper *helper, AVCodecContext *avCodecContext,
                AVRational &base, double fps);

    ~VideoPlayer();

    void play() override;

    void decode() override;

    void stop() override;

    void setWindow(ANativeWindow *window);

    AudioPlayer *audioPlayer = NULL;

private:
    double fps;
    pthread_t decodeThread;
    pthread_t playThread;
    ANativeWindow *window = NULL;
    pthread_mutex_t windowMutex;


    void _decode();

    void _play();

    void onDraw(uint8_t *data[4], int linesize[4], int width, int height);
};


#endif //ICANLIVE_VIDEOPLAYER_H
